package org.transfer.service;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.transfer.utils.ConfigUtil;
import org.transfer.utils.SpringContextHolder;

public class MonitorProducer {

	public static void init() {
		try {
			String period = ConfigUtil.getConfigure().getProperty("period");
			String time = ConfigUtil.getConfigure().getProperty("time");
			Runnable runnable = new Runnable() {
				public void run() {
					try {
						long start = System.currentTimeMillis();
						action();
						long end = System.currentTimeMillis();
						System.out.println("执行时间：" + (end - start));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
			ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
			// 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
			service.scheduleAtFixedRate(runnable, 0, Integer.valueOf(period), TimeUnit.valueOf(time));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public static void action() {

		DecryptFileService decryptFileService = SpringContextHolder.getBean(DecryptFileService.class);
		DecompressionService decompressionService = SpringContextHolder.getBean(DecompressionService.class);
		OriginalSqlServerService originalSqlServerService = SpringContextHolder.getBean(OriginalSqlServerService.class);
		OriginalMySqlService originalMySqlService = SpringContextHolder.getBean(OriginalMySqlService.class);
		FilterFileService filterFileService = SpringContextHolder.getBean(FilterFileService.class);
		HandlingDataService handlingDataService = SpringContextHolder.getBean(HandlingDataService.class);

		try {
			// 扫描过滤处理文件
			filterFileService.filterFile();
			// 扫描加密文件
			decryptFileService.decryptFile();
			// 扫描压缩文件
			decompressionService.decompression();
			// 扫描文件sqlServer恢复
			originalSqlServerService.recoveryData();
			// 扫描文件mySql恢复
			originalMySqlService.recoveryData();
			// 扫描文件数据处理
			handlingDataService.handlingData();

		} catch (Exception e) {
			System.out.println("程序执行失败！");
			e.printStackTrace();
		}
	}

}
